//
//  FLLHomeViewController.m
//  FLLGodOfCookery
//
//  Created by LL.F on 16/8/27.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLHomeViewController.h"
#import "FLLMacro.h"
#import "FLLClassifyViewController.h"
#import "UIView+Frame.h"
#import "FLLRecommendViewController.h"
#import "FLLMaterialViewController.h"
#import "FLLSearchViewController.h"

@interface FLLHomeViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UICollectionView *homeCollectionView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIViewController *currentViewController;
// navigationBar的自定义titleView
@property (nonatomic, strong) UIView *titleView;
// titleArray
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *selectedButton;
@end

@implementation FLLHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建子视图控制器
    [self createChildViewController];
    // 创建titleView
    self.titleArray = [NSArray arrayWithObjects:@"推荐", @"食材", @"分类", nil];
    [self createTitleView];
    // 创建scrollView
    [self createScrollView];
    // 创建barButton
    [self createBarButton];

}
- (void)createChildViewController
{
    FLLRecommendViewController *recommend = [[FLLRecommendViewController alloc] init];
    [self addChildViewController:recommend];
    FLLMaterialViewController *material = [[FLLMaterialViewController alloc] init];
    [self addChildViewController:material];
    FLLClassifyViewController *classify = [[FLLClassifyViewController alloc] init];
    [self addChildViewController:classify];
}
// 创建titleView
- (void)createTitleView
{
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = FLLOrangeColor;
    _lineView.fll_height = 3;
    for (int i = 0; i < _titleArray.count; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.frame = CGRectMake(_titleView.frame.size.width / 3 * i, 0, _titleView.frame.size.width / 3, _titleView.frame.size.height - 3);
        [self.titleView addSubview:titleButton];
        [titleButton setTitle:_titleArray[i] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateDisabled];
        titleButton.tag = 1000 + i;
        titleButton.titleLabel.font = [UIFont systemFontOfSize:17 * kWidthScale];
        [titleButton addTarget:self action:@selector(didClickedTitleButton:) forControlEvents:UIControlEventTouchUpInside];
        _lineView.fll_y = titleButton.frame.size.height;
        if (i == 0) {
            //   让按钮的lable根据内容计算尺寸
            [titleButton.titleLabel sizeToFit];
    
            [self didClickedTitleButton:titleButton];
            _lineView.fll_width = titleButton.titleLabel.fll_width;
            _lineView.fll_centerX = titleButton.center.x;
        }
    }
    [self.titleView addSubview:_lineView];
    self.navigationItem.titleView = _titleView;
    
}


// 创建ScrollView
- (void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    _scrollView.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    [self.view addSubview:_scrollView];

    [self.view insertSubview:_scrollView atIndex:0];
    [self scrollViewDidEndScrollingAnimation:_scrollView];

}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger i = scrollView.contentOffset.x / SCREEN_WIDTH;
//    NSLog(@"%ld", i);
    self.currentViewController = self.childViewControllers[i];
    _currentViewController.view.frame = CGRectMake(scrollView.contentOffset.x, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 44);
    [scrollView addSubview:_currentViewController.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    [self didClickedTitleButton:_titleView.subviews[index]];
}
- (void)didClickedTitleButton:(UIButton *)button
{

    self.selectedButton.enabled = YES;
    button.enabled = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.fll_width = button.titleLabel.fll_width;
        self.lineView.fll_centerX = button.fll_centerX;
    }];
    self.selectedButton = button;
    NSInteger i = button.tag - 1000;
    CGPoint index = self.scrollView.contentOffset;
    index.x = i * SCREEN_WIDTH;
    [self.scrollView setContentOffset:index animated:YES];
    
}

- (void)createBarButton
{
    // 扫一扫
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"saoyisao"] imageWithRenderingMode:1] style:UIBarButtonItemStylePlain target:self action:@selector(didSweepAction:)];
    // 搜索
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"search"] imageWithRenderingMode:1]style:UIBarButtonItemStylePlain target:self action:@selector(didSearchAction:)];
    
}

#pragma mark -- 扫一扫功能
- (void)didSweepAction:(UIBarButtonItem *)button
{
    
    
}
#pragma mark -- 搜索功能
- (void)didSearchAction:(UIBarButtonItem *)button
{
    FLLSearchViewController *searchVC = [[FLLSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
